package sitemaps;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.logging.Logger;

public class SitemapNotifier {
	
	private static final Logger LOG = Logger.getLogger(SitemapNotifier.class.getName());

	public SitemapNotifier(List<String> basePingUrls, String encoding) {
		this.basePingUrls = basePingUrls;
		this.encoding = encoding;
		stringLoader = new StringLoader(encoding);
	}

	private StringLoader stringLoader;
	private List<String> basePingUrls;
	private String encoding;
	
	public void ping(final String sitemapUrl) throws UnsupportedEncodingException, MalformedURLException, IOException {
		final String pingSite = URLEncoder.encode(sitemapUrl, encoding);
		for(String basePingUrl: basePingUrls) {
			final URL pingUrl = new URL(basePingUrl + pingSite);
			final URLConnection connection = pingUrl.openConnection();
			connection.connect();
			final InputStream is = connection.getInputStream();
			final String reply = stringLoader.load(is);
			LOG.info(reply);
		}
	}

	

}
